//
//  CPHomeViewController.swift
//  cockpit
//
//  Created by Haijun Si on 2020/7/7.
//  Copyright © 2020 chenganrt. All rights reserved.
//

import UIKit
import RxSwift

class CPHomeViewController: CPBaseViewController {

    private let viewModel = CPHomeViewModel()
    
    private let input = CPHomeViewModel.CPInput()
    
    private lazy var output = viewModel.transform(input: input)
    
    private let headView = CPHomeTopView(frame: CGRect(x: 0, y: 0, width: CPScreenWidth, height: 220 + CPScreenWidth/4.0))
    
    private let tableView = JFCTableView().then { (tb) in
        tb.canEdit = false
        tb.frame = CGRect(x: 0, y: 0, width: CPScreenWidth, height: CPScreenHeight)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    override func setViewUI() {
        self.view.addSubview(self.tableView)
        self.tableView.tableHeaderView = self.headView
        
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            automaticallyAdjustsScrollViewInsets = false
        }
    }
    
    override func loadData() {
        self.headView.dataArray = output.moreTypeArr.value
        self.headView.bannerImgs = output.banners.value
        bindViewModel()
    }
    
    override func setNavigationBarView() {
        self.isHidNavi = true
    }
}

extension CPHomeViewController {
    func bindViewModel() {
        output.dataSource
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [weak self] (vms) in
                guard let `self` = self else {
                    return
                }

                let section = JFCTableViewSection.init(key: "default", viewModels: vms ?? [])
                self.tableView.sections = [section]
            })
            .disposed(by: rx.disposeBag)

        tableView.didSelectRowCallback = { (vm, sectionModel) in
            print("点击了\(vm)---\(sectionModel)")
        }
        
//        tableView.viewForHeaderCallback = { (sectionModel) -> UIView in
//            
//        }
    }
}
